# -*- coding: UTF-8 -*-

'''
Created on 03.02.2013

@author: Peter Heß, Germany

samples from vvvv how they do it:

Centered/-/-/Phase=.50/Count=11
0.0455, 0.1364, 0.2273, 0.3182, 0.4091, -0.5000, -0.4091, -0.3182, -0.2273, -0.1364, -0.0455

Centered/-/-/Phase=.75/Count=11
0.2955, 0.3864, 0.4773, -0.4318, -0.3409, -0.2500, -0.1591, -0.0682, 0.0227, 0.1136, 0.2045

'''

import numpy as np
from value.Transform import CargoTransform

class BaseObject:
    '''
        Creates a output filled with linear values.
        Calling out, does not recompute the output. The output is always recomputed
        when one of the imput values are changed.
    '''
    
    def __init__(self):
        self.lastMaxCount = 0
        self.nextMaxCount = 0
        self.drawObjects = None
        
    def setup(self):
        '''
        manage the graphics objects
        '''
        # Fall 1 - es gibt noch keine Planes -> Planes anlegen -> weiter mit Fall 2
        # Fall 2 - die Anzahl der Planes entspricht self.maxcount -> planes in der gegebenen
        #            Reihenfolge aktualisieren
        # Fall 3 - Es gibt weniger -> Liste Redunzieren und dann weiter mit Fall 2
        # Fall 4 - Es gibt mehr -> Liste erweitern und dann weiter mit Fall 2
        if (self.drawObjects == None):
            objs = []
            for i in range(self.nextMaxCount):
                objs.append( self.create3DObject() )
            self.drawObjects = np.array( objs )
        
        lenObjs = len(self.drawObjects)
        
        if (lenObjs < self.nextMaxCount):
            #Fall 3 
            diff =   self.nextMaxCount - lenObjs
            for i in range(diff):
               self.drawObjects = np.append( self.drawObjects, [self.create3DObject()])
        
        elif (lenObjs > self.nextMaxCount):
            #Fall4 - some object must be removed
            self.drawObjects = self.drawObjects[:self.nextMaxCount]
        

    def create3DObject(self):
        return dumy()
                
    def update(self, transform=CargoTransform()):
        '''
        '''        

        maxcount = transform.maxcount 
        
        # let's now build a big spread
        # self.spread = np.array(self.translateX, self.translateY, self.scaleX, self.scaleY)
        
        if self.lastMaxCount <> maxcount:
            self.nextMaxCount = maxcount
            self.setup()
            
        # now all objects exitst and can be manipulated
        count = 0
        for dumyObj in self.drawObjects:
            dumyObj.setup( count, transform  )
            dumyObj.draw()
            count +=1
        
        # prepare the next computing slice    
        self.lastMaxCount = self.nextMaxCount
        
    # -----------------------------------------------------------------------------------------

    def dumpSpreads(self):
        '''Shows the internal arrays created in update()'''
        print "plane self.maxcount:", self.maxcount 
        print 'self.x:     ', self.scaleY
        print 'self.y:     ', self.translateY
        print 'self.width: ', self.scaleX
        print 'self.height:', self.scaleY

    # -----------------------------------------------------------------------------------------           
    
    def out(self):            
        return self.output

    # -----------------------------------------------------------------------------------------

class dumy(object):

    def __init__(self):
        print "create dumy"

    def setup(self, ordinalNr, transform):    
        self.ordinalNr = ordinalNr
        self.transform = transform
        
    def draw(self): #only for testing
        tx = self.transform.translateX[self.ordinalNr]
        ty = self.transform.translateY[self.ordinalNr]
        print "draw():",self," translateX:", tx, "translateY:", ty  
    